package com.ceba.test.modules.statistic.dao;

import java.util.Date;
import java.util.Map;

import com.ceba.base.helper.DBJSONHelper;
import com.ceba.cebautils.DateUtil;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.enums.EditionTypeEnum;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.base.web.UserUtils;
import com.ceba.test.modules.statistic.dao.sqlconstants.StatisticSqlConstants;

import abc.IContextDictionary;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.web.HttpContext;
import org.json.JSONObject;

public class StatisticDao extends IBaseDao {

	public String statisticSign(HttpContext context, IContextDictionary dic) {
		SqlDataSource dataSource = new SqlDataSource();
		//获取当前用户
		String userId = UserUtils.getUserId(context.getRequest());
		String key = (String)dic.get("key");
		//获取是否为当前当前用户导出
		String isUser= (String)dic.get("isUser");
		
		if(key!=null){
			key = key.equals("") ? "-" : key;
		}
		String qtype = (String)dic.get("qtype");
		String startTime = (String)dic.get("startTime");
		String endTime = (String)dic.get("endTime");
		String timeCondition = "";
		if (StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			startTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && StringUtil.isEmptyString(endTime)) {
			endTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		}


		String type = (String)dic.get("type");
		Sql sql = null;

		StringBuffer ss = new StringBuffer();
		ss.append(StatisticSqlConstants.condition);
			if("3".equals(CebaConfig.USE_VERSION)){
				ss.append(StatisticSqlConstants.sqlCondition_report_sign);
				if (!StringUtil.isEmptyString(timeCondition)) {
					ss.append(" where cb_report_sign." + timeCondition);
				}
				if(isUser.equals("true")){
					ss.append(" where cb_report_sign.CREATE_USER='"+userId+"'");
				}
			}
		ss.append(" )AS B1");
		if("search".equals(qtype)){
			ss.append(" ORDER BY CREATE_TIME DESC ");
				//	+ " LIMIT " + start + " , " + limit);
		}else {
			ss.append(" ORDER BY CREATE_TIME DESC ");
		}
		
		if(!"3".equals(CebaConfig.USE_VERSION)){
			ss =new StringBuffer(ss.toString().replace("'%?key%'", "'%"+(String)dic.get("key")+"%'"));
		}
		sql = new Sql(ss.toString());
		dataSource.getSelectCommands().add(sql);

		DataTable dt = dataSource.query(0,dic);
		
		if(dt !=null){
			return dt.toJSON();
		}else{
			return DBJSONHelper.getEmptyDBJSONString();
		}
	}
	
	public String statisticSignForDownLoad(String userId,Map<String,Object> dic) {
		String key = (String)dic.get("key");
		//获取是否为当前当前用户导出
		String isUser= (String)dic.get("isUser");
		
		if(key!=null){
			key = key.equals("") ? "-" : key;
		}
		String qtype = (String)dic.get("qtype");
		String startTime = (String)dic.get("startTime");
		String endTime = (String)dic.get("endTime");
		String timeCondition = "";
		if (StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			startTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && StringUtil.isEmptyString(endTime)) {
			endTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		}

		String type = (String)dic.get("type");
		Sql sql = null;

		StringBuffer ss = new StringBuffer();
		ss.append(StatisticSqlConstants.condition);
		// 附件

//			if("3".equals(CebaConfig.USE_VERSION)){
				ss.append(StatisticSqlConstants.sqlCondition_report_sign);
				if (!StringUtil.isEmptyString(timeCondition)) {
					ss.append(" where cb_report_sign." + timeCondition);
				}
				if(isUser.equals("true")){
					ss.append(" where cb_report_sign.CREATE_USER='"+userId+"' and cb_report_sign.EDITION_TYPE="+EditionTypeEnum.BE.getType());
				}
//			}

		ss.append(" )AS B1");
		if("search".equals(qtype)){
			ss.append(" ORDER BY CREATE_TIME DESC ");
				//	+ " LIMIT " + start + " , " + limit);
		}else {
			ss.append(" ORDER BY CREATE_TIME DESC ");
		}
		
		if(!"3".equals(CebaConfig.USE_VERSION)){
			ss =new StringBuffer(ss.toString().replace("'%?key%'", "'%"+(String)dic.get("key")+"%'"));
		}
		sql = new Sql(ss.toString());
		DataTable dt = SqlSessionManager.getDataTable(sql);
		
		if(dt !=null){
			return dt.toJSON();
		}else{
			return DBJSONHelper.getEmptyDBJSONString();
		}
	}
	
	public int getAllStatistisCount(String startTime,String endTime,String key){
		String timeCondition = "";
		if (StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			startTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && StringUtil.isEmptyString(endTime)) {
			endTime = DateUtil.dateTransFromCST(new Date().toString(), DateUtil.YYYY_MM_DD_HH_MM_SS);
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		} else if (!StringUtil.isEmptyString(startTime) && !StringUtil.isEmptyString(endTime)) {
			timeCondition = " CREATE_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'";
		}

		Sql sql = null;

		StringBuffer ss = new StringBuffer();
		if((EditionTypeEnum.BE.getType()+"").equals(CebaConfig.USE_VERSION) || (EditionTypeEnum.API.getType()+"").equals(CebaConfig.USE_VERSION)){
			ss.append(StatisticSqlConstants.condition);
			ss.append(StatisticSqlConstants.sqlCondition_report_sign);
			if (!StringUtil.isEmptyString(timeCondition)) {
				ss.append(" where cb_report_sign." + timeCondition);
			}
			ss.append(" )AS B1");
			ss.append(" ORDER BY CREATE_TIME DESC ");
			sql = new Sql(ss.toString());
			DataTable dt = SqlSessionManager.getDataTable(sql);
			if(dt !=null && dt.getTotal() >-1){
				return dt.getRowCount();
			}else{
				return 0;
			}
		}
		return 0;
	}

}
